/**
 * <p>Title: YiHaoUserDetailService.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2015</p>
 * <p>Company: SiySoft</p>
 * @author liguanghui
 * @date 2015年5月25日
 * @version 1.0
 */
package com.siysoft.tc.base.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserCache;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.siysoft.tc.base.bean.to.Manager;
import com.tc.base.dao.BaseDAO;

/**
 * <p>
 * Title: YiHaoUserDetailService
 * </p>
 * <p>
 * Description: 艺好学管理员信息Service
 * </p>
 * <p>
 * Company: SiySoft
 * </p>
 * 
 * @author liguanghui
 * @date 2015年5月25日
 */
public class AdminDetailService implements UserDetailsService {

	@Autowired
	private BaseDAO baseDAO;
	
	@Autowired  
	 private UserCache userCache;  

	protected final Logger logger = Logger
			.getLogger(AdminDetailService.class);

	/*
	 * （非 Javadoc） <p>Title: loadUserByUsername</p> <p>Description: </p>
	 * 
	 * @param arg0
	 * 
	 * @return
	 * 
	 * @throws UsernameNotFoundException
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetailsService#
	 * loadUserByUsername(java.lang.String)
	 */
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		//先从缓存中获取用户
		Manager user = (Manager) this.userCache.getUserFromCache(username);  
	
		// 如果缓存没命中
		if (user == null) {
			// 查询用户，匹配手机号或邮箱地址
			List<Manager> list = this.baseDAO.findByHQL("from Manager where username='"
					+ username + "'");
			// 获取结果
			if ((list != null) && (list.size() > 0)) {
				user = (Manager) list.get(0);
			}
			else{
				throw new UsernameNotFoundException("UsernameNotFound",
						new Object[] { username });
			}
		}
		
		Set<GrantedAuthority> grantedAuthorities = loadUserAuthorities(username);
		user.setAuthorities(grantedAuthorities);

		logger.info("*********************" + username
				+ "***********************");
		logger.info(user.getAuthorities());
		logger.info("********************************************************");
		
		return user;

	}

	public Set<GrantedAuthority> loadUserAuthorities(String username) {

		Set<GrantedAuthority> authSet = new HashSet();
		String sql = "SELECT sr.ROLE_NAME FROM db_manager su, db_role_manager sur,db_role sr"
				+ " \n\tWHERE su.MANAGER_ID = sur.MANAGER_ID AND sur.ROLE_ID = sr.ROLE_ID AND su.username = '"
				+ username + "'";
		List<String> list = new ArrayList();
		try {
			list = (List) this.baseDAO.findBySQL(sql, null, -1, -1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		for (String str : list) {
			GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(
					str);
			authSet.add(grantedAuthority);
		}

		return authSet;
	}

}
